﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Ext.Net;
using Ext.Net.MVC;
using WebEntities;
using WebEntities.Models.Framework;
using com.subb.WebFramework;

namespace WebsiteAdmin.Controllers
{
    public class HomeController : Controller
    {
        /// <summary>
        /// 后台首页加载的数据：
        /// 1.菜单和菜单项，目前只支持一级菜单项。
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult Index()
        {
            FW_MemberDAL memdal = new FW_MemberDAL();
            FW_Member member = memdal.GetMember(HttpContext.User.Identity.Name);
            string gid = member.GID;
            //获取菜单和菜单项
            FW_MenuDAL mudal = new FW_MenuDAL();
            List<FW_MemberMenu> memMenuList = mudal.GetAllMemberMenu(gid);
            FW_MenuItemDAL midal = new FW_MenuItemDAL();
            List<FW_MemberMuItem> memMuItemList = midal.GetAllMemberMuItem(gid);
            ViewBag.MemberMuItemList = memMuItemList;
            return View(memMenuList);
        }

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult VerifyUser(string txtUserName, string txtPassword)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                FW_MemberDAL mdal = new FW_MemberDAL();
                DateTime dtLastLogin;
                MemberVerifyResult vresult = mdal.VerifyMember(txtUserName, txtPassword, out dtLastLogin);
                if (vresult == MemberVerifyResult.Success)
                {
                    FW_Member member = mdal.GetMember(txtUserName);
                    if (member.MemType == (int)MemberType.WebAdmin)
                    {
                        response.Success = true;
                        response.ExtraParams.Add(new Parameter("Msg", "身份验证成功！"));
                        FormsAuthentication.SetAuthCookie(txtUserName, false);
                        HttpCookie cookie = new HttpCookie("loginuser", txtUserName);
                        cookie.Expires = DateTime.Now.AddDays(10);
                        Response.Cookies.Add(cookie);
                        Session["LastLoginTime"] = dtLastLogin.ToString();
                        mdal.LoginSuccess(txtUserName);
                    }
                    else
                    {
                        response.Success = false;
                        response.ExtraParams.Add(new Parameter("Msg", "不是后台管理帐号，不能登录系统！"));
                    }
                }
                else if (vresult == MemberVerifyResult.Locked)
                {
                    response.Success = false;
                    response.ExtraParams.Add(new Parameter("Msg", "该帐号已被冻结，不能登录系统！"));
                }
                else
                {
                    response.Success = false;
                    response.ExtraParams.Add(new Parameter("Msg", "找不到用户或者密码错误！"));
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                //response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                response.ExtraParams.Add(new Parameter("Msg", "登录错误，请与系统管理员联系。错误原因：" + ex.Message));
            }
            return response;
        }

        [Authorize]
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Login");
        }

        [Authorize]
        public ActionResult LoginUserInfo()
        {
            Member member = new Member();
            if (Request.IsAuthenticated)
            {
                FW_MemberDAL mbdal = new FW_MemberDAL();
                FW_Member fwmember = mbdal.GetMember(HttpContext.User.Identity.Name);
                member = Member.AsMember(fwmember);
            }
            return View(member);
        }
    }
}
